package com.jwthhealth_pub.old.report.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.report.model.resbody.SuggFoodRes;
import com.jwthhealth_pub.old.report.view.adapter.FoodSuggAdapter;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;

/**
 * 饮食建议
 * Created by liuzhen on 2017/10/23
 */

public class FoodSuggestActivity extends BaseActivity {
    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.rv_date)
    RecyclerView rvData;
    @BindView(R.id.desc)
    TextView desc;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setFitsSystemWindows(true);

        setContentView(R.layout.activity_diet_suggest_activity);
        ButterKnife.bind(this);

        getData();
    }

    @Override
    public void title() {
        titleLayout.setTitle(getResources().getString(R.string.report_title_new_report_diet_suggest));
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void getData() {
        final String id = getIntent().getStringExtra("id");
        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        if (userinfoModel == null) return;

        final String uid = userinfoModel.getId();
        if (uid == null) return;

        String token = userinfoModel.getAndroidtoken();
        if (token == null) return;

//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                ApiHelper.request(ApiHelper.BASE_URL+"hra-new/get-desc?id="+id+"&uid="+uid+"&dev="+ApiHelper.deviceId+"&mac="+ApiHelper.macAddress+"&sign=d7523f84e1128488b2c6c92c98bf5d81");
//            }
//        }.start();

        final Call<SuggFoodRes> _call = ApiHelper.getFoodSugg(id, uid, token);
        Observable<SuggFoodRes.DataBean> observable = Observable.create(new ObservableOnSubscribe<SuggFoodRes.DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<SuggFoodRes.DataBean> e) throws Exception {
                showLoadProgresbar();
                SuggFoodRes body = _call.execute().body();
                if (body != null) {
                    if (body.getCode().equals("0")) {
                        e.onNext(body.getData());
                    } else {
                        e.onError(new Exception(body.getMsg()));
                    }
                } else {
                    e.onError(new Exception(getString(R.string.common_net_error)));
                }
            }
        });

        Observer<SuggFoodRes.DataBean> observer = new Observer<SuggFoodRes.DataBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SuggFoodRes.DataBean o) {
                missLoadProgressbar();
                renderingUi(o);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.toast(e.getMessage());
                missLoadProgressbar();
            }

            @Override
            public void onComplete() {
                missLoadProgressbar();
            }
        };

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

    private void renderingUi(SuggFoodRes.DataBean data) {
        if (data == null || data.getSon() == null || data.getSon().size() <= 0) return;
        rvData.setLayoutManager(new LinearLayoutManager(this));
        rvData.setAdapter(new FoodSuggAdapter(this, data.getSon()));

        // TODO: 2017/10/26
        desc.setText(data.getDesc());
    }
}
